﻿using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;

namespace Isuka.Excel
{
    /// <summary>
    /// Excelワークブック
    /// </summary>
    public class XWorkbook
        : ExcelObject
    {
        readonly XWorksheets sheets;

        public XWorkbook(XApplication application)
            : this(application, application.ActiveBook)
        {
            Contract.Requires(application != null);
            Contract.Requires(application.ActiveBook != null);
        }

        /// <summary>
        /// コンストラクター
        /// </summary>
        /// <param name="application"></param>
        /// <param name="workbook"></param>
        internal XWorkbook(XApplication application, object workbook)
            : base(application, workbook)
        {
            Contract.Requires(application != null);
            Contract.Requires(workbook != null);

            this.sheets = new XWorksheets(this, Object.Worksheets);
        }

        /// <summary>
        /// ワークシートを取得
        /// </summary>
        public XWorksheets Worksheets
        {
            get
            {
                Contract.Ensures(Contract.Result<XWorksheets>() != null);
                VerifyNotDisposed();
                return sheets;
            }
        }

        #region AutomationObject メンバー

        protected override void PreReleaseObject()
        {
            Object.Close(false);
            base.PreReleaseObject();
        }

        #endregion

        #region ContractInvariantMethod

        [ContractInvariantMethod]
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
        private void ObjectInvariant()
        {
            Contract.Invariant(sheets != null);
        }

        #endregion

    }
}
